package FlexTrade;

import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import net.jini.config.Configuration;
import net.jini.config.ConfigurationProvider;
import net.jini.core.entry.Entry;
import net.jini.core.lookup.ServiceID;
import net.jini.discovery.LookupDiscovery;
import net.jini.discovery.LookupDiscoveryManager;
import net.jini.export.Exporter;
import net.jini.lease.LeaseRenewalManager;
import net.jini.lookup.JoinManager;
import net.jini.lookup.ServiceIDListener;
import net.jini.lookup.entry.Name;

/*
 *main application 
 */

public class Market implements ServiceIDListener {

	//==================================================
	//PUBLIC
	  public static String CONFIG_FILE = "/home/hieuht/Eclipse/workspaceJEE/FlexTrade/src/FlexTrade/configFile.config";
	  public static String POLICY_FILE = "/home/hieuht/Eclipse/workspaceJEE/FlexTrade/src/FlexTrade/policy.txt";
	 
	  public static String MARKET_SERVICE_NAME = "FlexTradeService";
	  public static String AUTHENTICATE_SERVICE_NAME = "AuthenticateService";
	  public static String REMOTE_ACCOUNTS_NAME = "RemoteAccountsName";
	//==================================================
	
	protected MarketServiceImpl market_service;
	protected Remote proxy;
	
	protected AuthenticateImpl authenticate_service;
	protected Remote authenticateProxy;
	protected RemoteAccountsImpl remote_accounts;
	protected Remote remoteAccountsProxy;
	
	/**
	 * @param args
	 */
	
	public static void main(String[] args) {
		System.setProperty("java.security.policy",POLICY_FILE);
		System.setSecurityManager(new RMISecurityManager());
		System.setProperty("java.rmi.server.codebase", "http://localhost:9000/");
		new Market();
		// stay around forever
		Object keepAlive = new Object();

		synchronized(keepAlive) {
			try {
				keepAlive.wait();
			} catch(InterruptedException e) {
				// do nothing  
			}
		}
	}
	
	
	public Market()	{
		try {
			market_service = new MarketServiceImpl();	// market service
			authenticate_service = new AuthenticateImpl();	//authentication service
			remote_accounts = new RemoteAccountsImpl();	//remote bank service
		} catch(Exception e) {
			e.printStackTrace();
		}
		String[] configArgs = new String[] {CONFIG_FILE};
		try {
			// get the configuration (by default a FileConfiguration)
			Configuration config =  ConfigurationProvider.getInstance(configArgs);
			// and use this to construct an exporter
			Exporter exporter1 = (Exporter) ((net.jini.config.Configuration) config).getEntry(
			"JeriExport", "exporter", Exporter.class);
			// export an object of this class
			proxy = exporter1.export(market_service);
			//export authentication class
			Exporter exporter2 = (Exporter) ((net.jini.config.Configuration) config).getEntry(
			"JeriExport", "exporter", Exporter.class);
			authenticateProxy = exporter2.export(authenticate_service);
			//export remote bank class
			Exporter exporter3 = (Exporter) ((net.jini.config.Configuration) config).getEntry(
			"JeriExport", "exporter", Exporter.class);
			remoteAccountsProxy = exporter3.export(remote_accounts);
			remote_accounts.setProxy(remoteAccountsProxy);
		} catch(Exception e) {
			System.err.println(e.toString());
			e.printStackTrace();
			System.exit(1);
		}
		try {
			LookupDiscoveryManager mgr = new LookupDiscoveryManager(LookupDiscovery.ALL_GROUPS,
					null, // unicast locators
					null); // DiscoveryListener
			//template
			
			//register market service to LUS
			Entry[] serverAttributes1 = new Entry[1];
		    serverAttributes1[0] = new Name (Market.MARKET_SERVICE_NAME);
		    new JoinManager(proxy, // service proxy
				    serverAttributes1, // attr sets
					this, // ServiceIDListener
					mgr, // DiscoveryManager
					new LeaseRenewalManager());
		    
		    //register Authentication service to LUS
			Entry[] serverAttributes2 = new Entry[1];
		    serverAttributes2[0] = new Name (Market.AUTHENTICATE_SERVICE_NAME);
		    new JoinManager(authenticateProxy, // service proxy
				    serverAttributes2, // attr sets
					this, // ServiceIDListener
					mgr, // DiscoveryManager
					new LeaseRenewalManager());
			
		    //register remote bank service to LUS
			Entry[] serverAttributes3 = new Entry[1];
		    serverAttributes3[0] = new Name (Market.REMOTE_ACCOUNTS_NAME);
		    new JoinManager(remoteAccountsProxy, // service proxy
				    serverAttributes3, // attr sets
					this, // ServiceIDListener
					mgr, // DiscoveryManager
					new LeaseRenewalManager());
		} catch(Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	@Override
	public void serviceIDNotify(ServiceID serviceID) {
		// called as a ServiceIDListener
		System.out.println("Service " + serviceID.toString() + " is successfully registered");
	}
}
